package utils;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * @author xuguoqing
 * @version 1.0
 * @description: 7z工具类
 * @date 2022/10/11 10:17
 */
public class SevenZUtil {
    /**
     * 7z 压缩.
     * @param srcDirPath 待压缩目录.
     * @param tarFilePath 压缩文件路径.
     * @throws Exception .
     */
   /* public static void sevenZCompress(String srcDirPath, String tarFilePath) throws Exception {
        File sevenZFile = new File(srcDirPath);
        if (!sevenZFile.exists()) {
            throw new Exception(srcDirPath + "所指文件不存在");
        }
        File sevenZTarFile = new File(tarFilePath);
        if(!sevenZTarFile.exists()){
            FileUtil.fileProber(sevenZTarFile);
        }
        SevenZArchiveEntry archiveEntry = null;
        SevenZOutputFile sevenZOutputFile = null;
        File[] sevenZFileList = null;
        FileInputStream sevenZFis = null;
        BufferedInputStream sevenBis = null;
        try {
            sevenZOutputFile = new SevenZOutputFile(sevenZTarFile);
            sevenZFileList = sevenZFile.listFiles();
            if (null == sevenZFileList || sevenZFileList.length <= 0) {
                return;
            }
            for (File file : sevenZFileList) {
                sevenZFis = new FileInputStream(file);
                sevenBis = new BufferedInputStream(sevenZFis);
                archiveEntry = sevenZOutputFile.createArchiveEntry(file, file.getName());
                sevenZOutputFile.putArchiveEntry(archiveEntry);
                int len = -1;
                // 将源文件写入到7z文件中.
                byte[] buf = new byte[1024];
                while ((len = sevenBis.read(buf)) != -1) {
                    sevenZOutputFile.write(buf, 0, len);
                }
            }
            sevenBis.close();
            sevenZFis.close();
            sevenZOutputFile.closeArchiveEntry();
        } finally {
            if (null != sevenBis) {
                sevenBis.close();
            }
            if (null != sevenZFis) {
                sevenZFis.close();
            }
            if (null != sevenZOutputFile) {
                sevenZOutputFile.close();
            }
        }
    }
*/
    /**
     * 7z 解压.
     * @param srcFilePath 待解压文件路径.
     * @param tarDirPath 解压后文件目录.
     * @throws Exception .
     */
    public static void sevenZDecompress(String srcFilePath, String tarDirPath) throws Exception {
        SevenZArchiveEntry archiveEntry = null;
        File sevenZSrcFile = null;
        SevenZFile sevenZFile = null;
        OutputStream sevenZOs = null;
        OutputStream sevenZBos = null;

        try {
            sevenZSrcFile = new File(srcFilePath);
            // 针对压缩文件开始解压.
            sevenZFile = new SevenZFile(sevenZSrcFile);
            File file = null;
            while ((archiveEntry = sevenZFile.getNextEntry()) != null) {
                if (!archiveEntry.isDirectory()) {
                    file = new File(tarDirPath, archiveEntry.getName());
                    // 创建此文件的上级目录.
                    if (!file.exists()) {
                        new File(file.getParent()).mkdirs();
                    }
                    sevenZOs = new FileOutputStream(file);
                    sevenZBos = new BufferedOutputStream(sevenZOs);
                    int len = -1;
                    byte[] buf = new byte[1024];
                    while ((len = sevenZFile.read(buf)) != -1) {
                        sevenZOs.write(buf, 0, len);
                    }
                }
            }
        } finally {
            if (null != sevenZOs) {
                sevenZOs.close();
            }
            if (null != sevenZBos) {
                sevenZBos.close();
            }
            if (null != sevenZFile) {
                sevenZFile.close();
            }
        }
    }

}
